package com.example.designsoft.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

/**
 * author : Gyk
 * time   : 2020/10/14
 * todo   :自定义惯性滑动ScrollView
 */
public class CustomScrollView extends ViewGroup {

    private int mScreenHeight;

    private int mLastY;
    private int mStart;
    private int mEnd;
    private Scroller mSroller;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mSroller = new Scroller(context);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
        Log.i("CustomScrollView", "mScreenHeight--->" + mScreenHeight);
    }

    @Override
    protected void onLayout(boolean b, int l, int i1, int i2, int i3) {
        int childCount = getChildCount();
        //设置ViewGroup的高度
        int totalHeight=0;
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mScreenHeight * childCount;//ViewGroup的高度为子View的高度*子View的数量<子View高度为一屏的情况下>
        setLayoutParams(mlp);


        for (int j = 0; j < childCount; j++) {
            View child = getChildAt(j);

            if (child.getVisibility() != View.GONE) {
                totalHeight =totalHeight+ getChildAt(j).getMeasuredHeight();
//                if (j==0){
//                    child.layout(l, 0, i2, totalHeight);
//                }else {
//                    child.layout(l,getChildAt(j-1).getMeasuredHeight(),i2,totalHeight);
//                }
                child.layout(l, j * mScreenHeight, i2, (j + 1) * mScreenHeight);//修改每个子View的top和bottom这两个属性，让它们能依次排列下来
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = y;
                mStart = getScrollY();
                Log.i("CustomScrollView", "ACTION_DOWN--->mLastY-->" + mLastY);
                break;
            case MotionEvent.ACTION_MOVE:
                if (!mSroller.isFinished()) {
                    mSroller.abortAnimation();
                }
                int dy = mLastY - y;
                if (getScrollY() < 0) {
                    dy = 0;
                }
                if (getScrollY() > mScreenHeight) {
                    dy = 0;
                }
                Log.i("CustomScrollView", "getScrolly--->" + getScrollY());
                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if (dScrollY > 0) {
                    if (dScrollY < mScreenHeight / 3) {
                        //如果不超过1/3,
                        // 就返回原位置(-dScrolly表示向下滑动,所以参数的意思就是向上滑动了dScrollY距离但是不想切换下一个view,
                        // 要让当前view回到原位置,所以要向下滑动dScrollY距离才能回到原点)
                        mSroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        //如果超过了1/3,就可以切换到下个view了,所以要向上滑动 (屏幕的高度-已经滑动的距离)这样就能切换到下一个view了
                        mSroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);
                    }
                } else {
                    if (-dScrollY < mScreenHeight / 3) {
                        mSroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        mSroller.startScroll(0, getScrollY(), 0, -mScreenHeight - dScrollY);
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mSroller.computeScrollOffset()) {
            scrollTo(0, mSroller.getCurrY());
            postInvalidate();
        }
    }
}
